<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="form.data.id ? '修改活动计划' : '新增活动计划'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="150">
          <FormItem prop="planStep" label="工作阶段">
            <Input v-model="form.data.planStep" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="planContent" label="工作内容">
            <Input v-model="form.data.planContent" placeholder="请输入"></Input>
          </FormItem>
          <FormItem label="时间" required>
            <Row>
              <Col span="24">
                <Row>
                  <Col span="11">
                    <FormItem prop="planStartTime">
                      <DatePicker type="datetime" style="width:100%" :options="options.planStartTime" placeholder="选择时间" v-model="form.data.planStartTime"></DatePicker>
                    </FormItem>
                  </Col>
                  <Col span="2" class="tc">-</Col>
                  <Col span="11">
                    <FormItem prop="planEndTime">
                      <DatePicker type="datetime" style="width:100%" :options="options.planEndTime" placeholder="选择时间" v-model="form.data.planEndTime"></DatePicker>
                    </FormItem>
                  </Col>
                </Row>
              </Col>
            </Row>
          </FormItem>
          <FormItem prop="chargePerson" label="负责人">
            <Input v-model="form.data.chargePerson" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="mobile" label="负责人联系电话">
            <Input v-model="form.data.mobile" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="staff" label="工作人员">
            <Input v-model="form.data.staff" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="works" label="内容描述">
            <Input type="textarea" v-model="form.data.works" placeholder="请输入" :rows="4"></Input>
          </FormItem>
          <FormItem prop="remark" label="备注">
            <Input type="textarea" v-model="form.data.remark" placeholder="请输入" :rows="4"></Input>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import { mapGetters } from 'vuex'
  import activityApi from '@/api/activity'

  export default {
    name: 'update-modal',
    props: [
      'form'
    ],
    data () {
      return {
        loading: true,
        ruleValidate: {
          planStep: [
            { required: true, message: '请输入工作阶段', trigger: 'blur' },
            { min: 1, max: 50, message: '工作阶段字符长度应小于50', trigger: 'change' }
          ],
          planContent: [
            { required: true, message: '请输入工作内容', trigger: 'change' },
            { min: 1, max: 50, message: '工作内容字符长度应小于50', trigger: 'change' }
          ],
          planStartTime: [
            { required: true, type: 'date', message: '请选择开始时间', trigger: 'change' }
          ],
          planEndTime: [
            { required: true, type: 'date', message: '请选择结束时间', trigger: 'change' }
          ],
          chargePerson: [
            { required: true, message: '请输入负责人', trigger: 'change' },
            { min: 1, max: 50, message: '负责人字符长度应小于50', trigger: 'change' }
          ],
          mobile: [
            { required: true, message: '请输入负责人联系电话', trigger: 'change' },
            { max: 50, message: '负责人联系电话字符长度应小于50', trigger: 'change' }
          ],
          staff: [
            { max: 50, message: '工作人员字符长度应小于50', trigger: 'change' }
          ],
          works: [
            { max: 100, message: '内容描述字符长度应小于100', trigger: 'change' }
          ],
          remark: [
            { max: 100, message: '备注字符长度应小于100', trigger: 'change' }
          ]
        },
        options: {
          planStartTime: {},
          planEndTime: {}
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose',
      'form.data.planStartTime': function (val, oldVal) {
        this.options.planEndTime.disabledDate = (date) => {
          let isDisabled = false
          if (this.form.data.planStartTime) {
            isDisabled = date && date.valueOf() < this.form.data.planStartTime.getTime()
          }
          return isDisabled
        }
      },
      'form.data.planEndTime': function (val, oldVal) {
        this.options.planStartTime.disabledDate = (date) => {
          let isDisabled = false
          if (this.form.data.planEndTime) {
            isDisabled = date && date.valueOf() > this.form.data.planEndTime.getTime()
          }
          return isDisabled
        }
      }
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$refs.form.resetFields()
        }
      },
      async submitForm () {
        let activityId = this.$route.params.activityId
        this.form.data.pkActBaseInfo = activityId
        if (this.form.data.id) {
          const { body } = await activityApi.plan.update(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('修改成功')
            this.form.visible = false
            this.$emit('submit')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
              this.loading = false
            }, 500)
          }
        } else {
          const { body } = await activityApi.plan.create(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('创建成功')
            this.form.visible = false
            this.$emit('submit')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
              this.loading = false
            }, 500)
          }
        }
      }
    }
  }
</script>
